package huixue.cn.com.rxapp.rx;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.support.v7.widget.AppCompatImageView;

import butterknife.BindView;
import huixue.cn.com.library.util.PixelUtils;
import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.base.SimpleActivity;

/**
 * 底片效果
 * 255-原来rgb值
 */
public class BitmapColorActivity3 extends SimpleActivity {

    @BindView(R.id.iv_img1)
    AppCompatImageView image1;

    @BindView(R.id.iv_img2)
    AppCompatImageView image2;

    @BindView(R.id.iv_img3)
    AppCompatImageView image3;

    @BindView(R.id.iv_img4)
    AppCompatImageView image4;

    private Bitmap bitmap,originBmp;

    @Override
    public int getLayout() {
        return R.layout.activity_bitmap_color3;
    }

    @Override
    public void initView() {
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.timg);
        float scale = PixelUtils.getScreenWidth() * 1.0f / bitmap.getWidth();
        Matrix matrix = new Matrix();
        matrix.setScale(scale, scale);
        originBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

        image1.setImageBitmap(originBmp);
    }

    @Override
    public void addEvent() {
        Bitmap bitmap2 = dipianxiaoguo();

        image2.setImageBitmap(bitmap2);


        Bitmap bitmap3 = naozhaopianxiaoguo();
        image3.setImageBitmap(bitmap3);

        Bitmap bitmap4 = fudiaoxiaoguo();
        image4.setImageBitmap(bitmap4);

    }

    private Bitmap naozhaopianxiaoguo(){
        int width = originBmp.getWidth();
        int height = originBmp.getHeight();

        int color,r,g,b,a;

        Bitmap bmp = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);

        int[] oldPx = new int[width*height];

        int[] newPx = new int[width*height];

        originBmp.getPixels(oldPx,0,width,0,0,width,height);

        for(int i=0;i<width*height;i++){
            color = oldPx[i];
            r = Color.red(color);
            g = Color.green(color);
            b = Color.blue(color);
            a = Color.alpha(color);

            r = (int)(0.393*r+0.769*g +0.189*b);
            g = (int)(0.349*r+0.686*g +0.168*b);
            b = (int)(0.272*r+0.534*g +0.131*b);

            if(r>255){
                r = 255;
            }else if(r<0){
                r = 0;
            }

            if(g>255){
                g = 255;
            }else if(g<0){
                g = 0;
            }

            if(b>255){
                b = 255;
            }else if(b<0){
                b = 0;
            }

            color = Color.argb(a,r,g,b);

            newPx[i] = color;
        }

        bmp.setPixels(newPx,0,width,0,0,width,height);

        return bmp;
    }


    private Bitmap dipianxiaoguo(){
        int width = originBmp.getWidth();
        int height = originBmp.getHeight();

        int color,r,g,b,a;

        Bitmap bmp = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);

        int[] oldPx = new int[width*height];

        int[] newPx = new int[width*height];

        originBmp.getPixels(oldPx,0,width,0,0,width,height);

        for(int i=0;i<width*height;i++){
            color = oldPx[i];
            r = Color.red(color);
            g = Color.green(color);
            b = Color.blue(color);
            a = Color.alpha(color);

            r = 255-r;
            g = 255-g;
            b = 255-g;
            a = a;

            if(r>255){
                r = 255;
            }else if(r<0){
                r = 0;
            }

            if(g>255){
                g = 255;
            }else if(g<0){
                g = 0;
            }

            if(b>255){
                b = 255;
            }else if(b<0){
                b = 0;
            }

            color = Color.argb(a,r,g,b);

            newPx[i] = color;
        }

        bmp.setPixels(newPx,0,width,0,0,width,height);

        return bmp;
    }

    /**
     * 浮雕效果
     * @return
     */
    private Bitmap fudiaoxiaoguo(){
        int width = originBmp.getWidth();
        int height = originBmp.getHeight();

        int color,r,g,b,a;

        int bcolor,r1,g1,b1;

        Bitmap bmp = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);

        int[] oldPx = new int[width*height];

        int[] newPx = new int[width*height];

        originBmp.getPixels(oldPx,0,width,0,0,width,height);

        for(int i=1;i<width*height;i++){
            bcolor = oldPx[i-1];
            r = Color.red(bcolor);
            g = Color.green(bcolor);
            b = Color.blue(bcolor);
            a = Color.alpha(bcolor);


            color = oldPx[i];

            r1 = Color.red(color);
            g1 = Color.red(color);
            b1 = Color.red(color);


            r = (r-r1+127);
            g = (g-g1+127);
            b = (b-b1+127);

            if(r>255){
                r = 255;
            }else if(r<0){
                r = 0;
            }

            if(g>255){
                g = 255;
            }else if(g<0){
                g = 0;
            }

            if(b>255){
                b = 255;
            }else if(b<0){
                b = 0;
            }

            color = Color.argb(a,r,g,b);

            newPx[i] = color;
        }

        bmp.setPixels(newPx,0,width,0,0,width,height);

        return bmp;
    }
}
